package com.shuoyue.ycgk.ui.estimate;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.JobInfo;
import com.shuoyue.ycgk.entity.UserTestPaper;
import com.shuoyue.ycgk.entity.address.AddressBean;
import com.shuoyue.ycgk.ui.questionbank.practice.estamate.EstamateQuestionActivity;
import com.shuoyue.ycgk.ui.system.SystemAddressContract;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EsSelecctJobActivity extends BaseMvpActivity<EstimateSelectContract.Presenter>
        implements EstimateSelectContract.View {

    int evaId, testId, memberEvaId;
    String name;

    public static void start(Context context, int evaId, int testId, int memberEvaId, String name) {
        context.startActivity(new Intent(context, EsSelecctJobActivity.class)
                .putExtra("evaId", evaId)
                .putExtra("testId", testId)
                .putExtra("name", name)
                .putExtra("memberEvaId", memberEvaId)
        );
    }

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.unit)
    Spinner unit;
    @BindView(R.id.jobs)
    Spinner jobs;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.regon)
    Spinner regon;

    SpinnerAdapter<String> regonAdapter;

    SpinnerAdapter<String> unitAdapter;

    SpinnerAdapter<JobInfo> jobAdapter;

    String selectRegon;
    String selectUnit;
    JobInfo selectJob;


    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_es_selecct_job;
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initView() {
        pageTitle.setText("估分报名");
        regonAdapter = new SpinnerAdapter<>(null, mContext);
        unitAdapter = new SpinnerAdapter<>(null, mContext);
        jobAdapter = new SpinnerAdapter<>(null, mContext);

        regon.setAdapter(regonAdapter);
        unit.setAdapter(unitAdapter);
        jobs.setAdapter(jobAdapter);
        regon.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (regonAdapter.getItem(position) == null) {
                    unit.setVisibility(View.INVISIBLE);
                    jobs.setVisibility(View.INVISIBLE);
                    selectUnit = null;
                    selectJob = null;
                    return;
                }
                if (selectRegon != null
                        && !selectRegon.equals(regon.getSelectedItem().toString())) {
                    unit.setVisibility(View.INVISIBLE);
                    jobs.setVisibility(View.INVISIBLE);
                    selectUnit = null;
                    selectJob = null;
                }
                selectRegon = regon.getSelectedItem().toString();
                mPresenter.getUnit(selectRegon, evaId);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        unit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (unitAdapter.getItem(position) == null) {
                    jobs.setVisibility(View.INVISIBLE);
                    selectJob = null;
                    return;
                }
                if (selectUnit != null
                        && !selectUnit.equals(unit.getSelectedItem().toString())) {
                    jobs.setVisibility(View.INVISIBLE);
                    selectJob = null;
                }

                selectUnit = unit.getSelectedItem().toString();
                mPresenter.getJob(selectRegon, selectUnit, evaId);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        jobs.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (((JobInfo) jobAdapter.getItem(position)).getPostName() == null) {
                    return;
                }
                selectJob = (JobInfo) jobAdapter.getItem(position);
                submit.setBackgroundResource(R.drawable.button_bg_theme);
                submit.setEnabled(true);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        evaId = getIntent().getIntExtra("evaId", -1);
        testId = getIntent().getIntExtra("testId", -1);
        memberEvaId = getIntent().getIntExtra("memberEvaId", -1);
        name = getIntent().getStringExtra("name");

        mPresenter = new EstimateSelectContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getAreas(evaId);
    }

    @OnClick({R.id.back, R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.submit:
                if (evaId != -1
                        && selectRegon != null
                        && selectJob != null) {

                    HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext, "提示", "确认报名");
                    hintConfirmDialog.setOkListener(v -> {
                        mPresenter.postChoose(selectJob.getId(), selectJob.getPostName(), evaId, selectRegon);
                        hintConfirmDialog.dismiss();
                    });
                    hintConfirmDialog.show();

                } else {
                    XToast.toast(mContext, "信息未完善,请选择");
                }
                break;
        }
    }

    @Override
    public void getUnitSuc(List<String> units) {
        units.add(0, null);
        unit.setVisibility(View.VISIBLE);
        unitAdapter.resetData(units);
        if (units != null && units.size() > 0) {
            selectUnit = units.get(0);
        }
    }

    @Override
    public void getAreaSuc(List<String> area) {
        area.add(0, null);
        regon.setVisibility(View.VISIBLE);
        regonAdapter.resetData(area);

    }

    @Override
    public void getJobSuc(List<JobInfo> job) {
        job.add(0, new JobInfo());
        jobs.setVisibility(View.VISIBLE);
        jobAdapter.resetData(job);
        if (job != null && job.size() > 0) {
            selectJob = job.get(0);
        }
    }

    @Override
    public void getPaperSuc(UserTestPaper userTestPaper) {
        userTestPaper.setName(name);
        EstamateQuestionActivity.start(mContext, userTestPaper, 0, userTestPaper.getTag());
        finish();
    }

    @Override
    public void postSuc() {
        mPresenter.getPaper(testId, memberEvaId);
    }


}
